home *** CD-ROM | disk | FTP | other *** search
/ Aminet 30 / Aminet 30 (1999)(Schatztruhe)[!][Apr 1999].iso / Aminet / misc / math / MathFX_src.lha / fxnxtv.c < prev    next >
C/C++ Source or Header  |  1995-12-20  |  8KB  |  264 lines

  1. /* Draws the next view of a 3-d plot. The physical coordinates of */
  2. /* the points for the next view are placed in the n points of arrays  */
  3. /* u and v. The silhouette found so far is stored in the heap as a */
  4. /* set of m peak points. */
  5.  
  6. /* Define the heap size.  This can be increased if necessary.  Someday */
  7. /* I hope to just allocate a heap space of minimum required size.      */
  8. #define  heaps    15000
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include "mathfx.h"
  13. #include "declare.h"
  14.  
  15. static int m, x, y;
  16.  
  17. void fxnxtv(u,v,n,init)
  18. int u[], v[], n, init;
  19. {
  20.       int xx;
  21.       int i, j, first;
  22.       int sx1, sx2, sy1, sy2;
  23.       int su1, su2, sv1, sv2;
  24.       int cx, cy, px, py;
  25.       int seg, ptold, lstold, pthi, pnewhi, newhi, change, ochange;
  26.       
  27.       first = 1;
  28.       pnewhi = 0;
  29.       
  30.       /* For the initial set of points, just disfxay them and store them as */
  31.       /* the peak points. */
  32.  
  33.       if (init == 1) {
  34.          if( heap3 == NULL)  /* heap not yet allocated so ... */
  35.             if(( heap3 = (int *)malloc(heaps*sizeof(int))) == NULL)
  36.                fatal("Not enough heap space in FXNXTV.");
  37.          y = heaps - n;
  38.          x = y - n;
  39.          movphy(u[0],v[0]);
  40.          heap3[x] = u[0];
  41.          heap3[y] = v[0];
  42.          for (i=1; i<n; i++){
  43.            draphy(u[i],v[i]);
  44.            heap3[x+i] = u[i];
  45.            heap3[y+i] = v[i];
  46.          }
  47.          m = n;
  48.          return;
  49.       }
  50.  
  51.       /* Otherwise, we need to consider hidden-line removal problem. We scan */
  52.       /* over the points in both the old (i.e. heap3[x...] and heap3[y...]) and */
  53.       /* new (i.e. u[] and v[]) arrays in order of increasing x coordinate. */
  54.       /* At each stage, we find the line segment in the other array (if one */
  55.       /* exists) that straddles the x coordinate of the point. We */
  56.       /* have to determine if the point lies above or below the line segment, */
  57.       /* and to check if the below/above status has changed since the last */
  58.       /* point. */
  59.  
  60.       /* If init = 2 we do not update the view, this is useful for drawing */
  61.       /* lines on the graph after we are done fxotting points.  Hidden line */
  62.       /* removal is still done, but the view is not updated. */
  63.       xx = 0;
  64.       i = 0;
  65.       j = 0;
  66.  
  67.       /* (heap3[x+i], heap3[y+i]) is the i'th point in the old array */
  68.       /* (u[j], v[j])           is the j'th point in the new array */
  69.  
  70.       while (i < m || j < n) {
  71.  
  72.         /* The coordinates of the point under consideration are (px,py). */
  73.         /* The line segment joins (sx1,sy1) to (sx2,sy2). */
  74.  
  75.         /* "ptold" is true if the point lies in the old array. We set it */
  76.         /*  by comparing the x coordinates of the i'th old point */
  77.         /*  and the j'th new point, being careful if we have fallen past */
  78.         /*  the edges. Having found the point, load up the point and */
  79.         /*  segment coordinates appropriately. */
  80.  
  81.         ptold = ((heap3[x+i] < u[j] && i<m) || j>= n);
  82.         if (ptold) {
  83.           px = heap3[x+i];
  84.           py = heap3[y+i];
  85.           seg = j>0 && j<n;
  86.           if (seg) {
  87.             sx1 = u[j-1];
  88.             sy1 = v[j-1];
  89.             sx2 = u[j];
  90.             sy2 = v[j];
  91.           }
  92.         }
  93.         else {
  94.           px = u[j];
  95.           py = v[j];
  96.           seg = i>0 && i<m;
  97.           if (seg) {
  98.             sx1 = heap3[x+i-1];
  99.             sy1 = heap3[y+i-1];
  100.             sx2 = heap3[x+i];
  101.             sy2 = heap3[y+i];
  102.           }
  103.         }
  104.  
  105.         /* Now determine if the point is higher than the segment, using the */
  106.         /* logical function "above". We also need to know if it is */
  107.         /* the old view or the new view that is higher. "newhi" is set true */
  108.         /* if the new view is higher than the old */
  109.  
  110.         if(seg)
  111.             pthi = fxabv(px,py,sx1,sy1,sx2,sy2);
  112.         else
  113.             pthi = 1;
  114.  
  115.         newhi = (ptold && !pthi) || (!ptold && pthi);
  116.         change = (newhi && !pnewhi) || (!newhi && pnewhi);
  117.  
  118.         /* There is a new intersection point to put in the peak array if the */
  119.         /* state of "newhi" changes */
  120.  
  121.         if (first) {
  122.           movphy(px,py);
  123.           first = 0;
  124.           lstold = ptold;
  125.           if (init != 2) {
  126.             heap3[xx] = px;
  127.             heap3[xx+1] = py;
  128.             xx = xx + 2;
  129.           }
  130.           pthi = 0;
  131.           ochange = 0;
  132.         }
  133.         else if (change) {
  134.           /* Take care of special cases at end of arrays.  If init is 2 the */
  135.           /* endpoints are not connected to the old view. */
  136.           if (init==2 && ((!ptold && j==0)||(ptold && i==0))) {
  137.             movphy(px,py);
  138.             lstold = ptold;
  139.             pthi = 0;
  140.             ochange = 0;
  141.           }
  142.           else if (init==2 && ((!ptold && i>=m)||(ptold && j>=n))) {
  143.             movphy(px,py);
  144.             lstold = ptold;
  145.             pthi = 0;
  146.             ochange = 0;
  147.           }
  148.           /* If init is not 2 then we do want to connect the current line */
  149.           /* with the previous view at the endpoints.  */
  150.           /* Also find intersection point with old view. */
  151.           else  {
  152.             if (i == 0) {
  153.               sx1 = heap3[x];
  154.               sy1 = -1;
  155.               sx2 = heap3[x];
  156.               sy2 = heap3[y];
  157.             }
  158.             else if (i >= m) {
  159.               sx1 = heap3[x+m-1];
  160.               sy1 = heap3[y+m-1];
  161.               sx2 = heap3[x+m-1];
  162.               sy2 = -1;
  163.             }
  164.             else {
  165.               sx1 = heap3[x+i-1];
  166.               sy1 = heap3[y+i-1];
  167.               sx2 = heap3[x+i];
  168.               sy2 = heap3[y+i];
  169.             }
  170.  
  171.             if (j == 0) {
  172.               su1 = u[0];
  173.               sv1 = -1;
  174.               su2 = u[0];
  175.               sv2 = v[0];
  176.             }
  177.             else if (j >= n) {
  178.               su1 = u[n-1];
  179.               sv1 = v[n-1];
  180.               su2 = u[n];
  181.               sv2 = -1;
  182.             }
  183.             else  {
  184.               su1 = u[j-1];
  185.               sv1 = v[j-1];
  186.               su2 = u[j];
  187.               sv2 = v[j];
  188.             }
  189.  
  190.             /* Determine the intersection */
  191.             fx3cut(sx1,sy1,sx2,sy2,su1,sv1,su2,sv2,&cx,&cy);
  192.             if (cx == px && cy == py)  {
  193.               if (lstold && !ochange)
  194.                 movphy(px,py);
  195.               else 
  196.                 draphy(px,py); 
  197.  
  198.               if (init != 2) {
  199.                 heap3[xx] = px;
  200.                 heap3[xx+1] = py;
  201.                 xx = xx+2;
  202.               }
  203.               lstold = 1;
  204.               pthi = 0;
  205.             }
  206.             else {
  207.               if (lstold && !ochange)
  208.                  movphy(cx,cy);
  209.               else
  210.                  draphy(cx,cy);
  211.  
  212.               lstold = 1;
  213.               if (init != 2) {
  214.                 heap3[xx] = cx;
  215.                 heap3[xx+1] = cy;
  216.                 xx = xx+2;
  217.               }
  218.             }
  219.             ochange =1;
  220.           }
  221.         }
  222.  
  223.         /* If point is high then draw fxot to point and update view. */
  224.         if (pthi) {
  225.           if (lstold && ptold)
  226.             movphy(px,py);
  227.           else 
  228.             draphy(px,py); 
  229.  
  230.           if (init != 2) {
  231.             heap3[xx] = px;
  232.             heap3[xx+1] = py;
  233.             xx = xx+2;
  234.           }
  235.           lstold = ptold;
  236.           ochange = 0;
  237.         }
  238.  
  239.         pnewhi = newhi;
  240.  
  241.         if (ptold)
  242.           i = i+1;
  243.         else
  244.           j = j+1;
  245.  
  246.         if (xx>=x) fatal("Heap overflow in fxnxtv.");
  247.       }
  248.  
  249.       /* Transfer the peak points to the RHS of the heap */
  250.  
  251.       if(init != 2) {
  252.         m = xx/2;
  253.         xx = 0;
  254.         y = heaps - m;
  255.         x = y - m;
  256.         for(i=0; i<m; i++) {
  257.           heap3[x+i] = heap3[xx];
  258.           heap3[y+i] = heap3[xx+1];
  259.           xx = xx+2;
  260.         }
  261.       }
  262. }
  263.  
  264.